from rest_framework import serializers
from .models import AnalysisChatMessage, AnalysisChatSession


class AnalysisChatSerializer(serializers.Serializer):
    """企业战略分析助手聊天请求序列化器"""
    prompt = serializers.CharField(
        required=True,
        min_length=1,
        error_messages={"required": "请输入分析需求", "min_length": "分析需求不能为空"}
    )
    user_id = serializers.CharField(required=False, default="123456789")  # 可选，默认用户ID


# 消息序列化器（嵌套在会话中）
class AnalysisChatMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = AnalysisChatMessage
        fields = ['id', 'sender', 'content', 'created_at', 'tokens_used']
        read_only_fields = fields  # 所有字段都是只读的


# 会话序列化器（包含消息列表）
class AnalysisChatSessionSerializer(serializers.ModelSerializer):
    messages = AnalysisChatMessageSerializer(many=True, read_only=True)  # 嵌套消息列表

    class Meta:
        model = AnalysisChatSession
        fields = ['id', 'title', 'created_at', 'updated_at', 'is_archived', 'messages']
        read_only_fields = fields